package com.gjintel.framework.role.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gjintel.framework.cache.InitCacheData;
import com.gjintel.framework.constant.Constant;
import com.gjintel.framework.log.model.LogInfo;
import com.gjintel.framework.log.util.LogUtil;
import com.gjintel.framework.log.util.LogUtil.LogType;
import com.gjintel.framework.menu.model.RoleModel;
import com.gjintel.framework.role.model.Role;
import com.gjintel.framework.role.service.IRoleService;
import com.gjintel.framework.user.model.User;
import com.gjintel.framework.util.CommUtil;
import com.gjintel.sdk.base.dao.Page;
import com.gjintel.sdk.base.dao.Sort;
import com.gjintel.sdk.util.StringUtil;

@RequestMapping("/framework/role")
@Controller
public class RoleController {
	@Autowired
	private IRoleService roleService;

	/**
	 * @description 根据ID对角色信息进行添加、修改操作
	 * @author fengya
	 * @date 2015-7-23 下午04:40:09
	 * @param id
	 * @return
	 * @return ModelAndView
	 */
	@RequestMapping("/input")
	public ModelAndView input(String id) {
		ModelAndView mv = new ModelAndView("framework/role/role-input");
		if (!StringUtil.isEmpty(id)) {
			Role role = roleService.get(Role.class, id);
			mv.addObject("role", role);
		}
		return mv;
	}

	@RequestMapping("/listAll")
	@ResponseBody
	public JSONArray listAll() {
		List<Role> list = this.roleService.findAll();
		return JSONArray.fromObject(list);
	}

	@RequestMapping("/list")
	@SuppressWarnings("unchecked")
	@ResponseBody
	public JSONObject list(Page page, Role role, Sort sort) {
		this.roleService.findAll(page, role, sort);
		return CommUtil.getPageJSONData(page, true);
	}

	/**
	 * @方法名：save
	 * @功能说明：保存记录数据
	 * @author fengya
	 * @date 2014-5-30 上午09:28:49
	 * @return void
	 */
	@RequestMapping("/save")
	@ResponseBody
	public String save(HttpSession session, Role role) {
		User user = (User) session.getAttribute("loginUser");
		if (role != null) {
			boolean sign = false;
			if (StringUtil.isEmpty(role.getId())) {
				role.setId(null);
				role.setCreateTime(new Date());
				role.setCreaterName(user.getName());
				role.setCreaterId(user.getId());
				sign = true;
			}
			role.setUpdateTime(new Date());
			role.setUpdaterName(user.getName());
			role.setUpdaterId(user.getId());
			this.roleService.saveOrUpdate(role);
			if (sign) {
				LogUtil.addLog(new LogInfo("添加角色", user.getId(), user.getName(), String.valueOf(LogType.ADD.ordinal()), role.getClass().getSimpleName(), role.getId()));
			} else {
				LogUtil.addLog(new LogInfo("更新角色", user.getId(), user.getName(), String.valueOf(LogType.UPDATE.ordinal()), role.getClass().getSimpleName(), role.getId()));
			}
			return Constant.SAVE_SUCCESS;
		}
		return Constant.SAVE_UN_SUCCESS;
	}

	/**
	 * @description 删除角色
	 * @author fengya
	 * @date 2015-7-23 下午04:46:40
	 * @param deleteIds
	 * @return
	 * @throws Exception
	 * @return String
	 */
	@RequestMapping("/remove")
	@ResponseBody
	public String remove(String deleteIds) throws Exception {
		this.roleService.removeData(deleteIds);
		InitCacheData.getInstance().initRoleModelData();
		// InitCacheData.getInstance().initRoleFunctionData();
		return Constant.DEL_SUCCESS;
	}

	/**
	 * @description 得到所有的角色绑定的菜单信息
	 * @author fengya
	 * @date 2015-8-3 上午10:50:09
	 * @return void
	 */
	@RequestMapping("/roleMenus")
	@ResponseBody
	public JSONArray roleMenus(String id) {
		List<RoleModel> rmList = this.roleService.getMenuByRoleId(id);
		return JSONArray.fromObject(rmList);
	}

	/**
	 * @description 绑定菜单信息
	 * @author fengya
	 * @date 2015-8-3 上午11:02:58
	 * @return
	 * @return String
	 */
	@RequestMapping("/bindMenu")
	@ResponseBody
	public String bindMenu(HttpSession session, String id, String deleteIds) {
		User user = (User) session.getAttribute("loginUser");
		List<RoleModel> rmList = this.roleService.bindMenu(user, id, deleteIds);
		this.roleService.saveOrUpdate(rmList);
		InitCacheData.getInstance().initRoleModelData();
		return Constant.BIND_SUCCESS;
	}

	/**
	 * @description 移除角色绑定的用户信息
	 * @author fengya
	 * @date 2015-8-3 上午11:04:08
	 * @return
	 * @return String
	 */
	@RequestMapping("/removeBindUser")
	@ResponseBody
	public String removeBindUser(HttpSession session, String id, String deleteIds) {
		User user = (User) session.getAttribute("loginUser");
		this.roleService.removeBindUser(id, deleteIds);
		LogUtil.addLog(new LogInfo("删除角色绑定的用户信息", user.getId(), user.getName(), String.valueOf(LogType.DELETE.ordinal()), Role.class.getSimpleName(), deleteIds));
		return Constant.EXE_SUCCESS;
	}
	
	/**
	 * @description 获取除超级用户外的所有角色
	 * @author shy
	 * @date 2016-9-25
	 * @return
	 * @return String
	 */
	@RequestMapping("/findRoleListExcludeAdmin")
	@ResponseBody
	public JSONArray findRoleListExcludeAdmin(String id) {
		List<Role> rList = this.roleService.getRoleListExcludeAdmin();
		return JSONArray.fromObject(rList);
	}
	
}
